package cn.xpg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.xpg.entity.Store;
import cn.xpg.service.StoreService;
import cn.xpg.tools.PageSupport;

@Controller
public class StoreController {
	// private Logger logger = Logger.getLogger(StoreController.class);

	@Resource(name = "storeService")
	private StoreService storeService;

	// 分页查询店铺信息
	@RequestMapping("/getStoreList.do")
	public String getStoreList(HttpServletRequest request) {
		String store_name = request.getParameter("store_name");
		String pageIndex = request.getParameter("pageIndex");
		String sname=request.getParameter("sname");
		int pageSize = 3;
		int currentPageNo = 1;
		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		int totalCount = 0;
		totalCount = storeService.getStoreCount(store_name);
		System.out.println("查询店铺信息！！！！！！！！！！————>getStoreList");
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();

		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Store> storeList = null;
		storeList = storeService.getStoreList(store_name, (currentPageNo - 1) * pageSize, pageSize);

		request.setAttribute("storeList", storeList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("store_name", store_name);
		if(sname.equals("base")) {
			return "/jsp/base/store_list";
		}else {
			return "/jsp/system/manage_dp";
		}
		
	}

	// 新增店铺
	@RequestMapping("addStore.do")
	public String addStore(@Valid Store store) {
		System.out.println("新增店铺信息！！！！！！！！！！————>addStore");
		if (storeService.addStore(store) > 0) {
			return "redirect:/getStoreList.do";
		} else {
			return "/jsp/base/store_list";
		}
	}

	// 获取店铺Id
	@RequestMapping("getStoreId.do")
	public String getStoreId(HttpServletRequest request) {
		System.out.println("获取店铺Id！！！！！！！！！！————>getStoreId");
		int store_id = Integer.parseInt(request.getParameter("store_id"));
		Store store = storeService.getStoreId(store_id);
		request.setAttribute("store", store);
	    return "/jsp/base/store_update";
	}
	
	//修改店铺
	@RequestMapping("updateStore.do")
	public String updateStore(@Valid Store store,HttpServletRequest request) {
		System.out.println("修改店铺信息！！！！！！！！！！————>updateStore");
		if(storeService.updateStore(store)>0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getStoreList.do?sname=base";
		}else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("store", store);
			return "/jsp/base/store_update";
		}
	}
	
	//删除店铺
	@RequestMapping("deleteStore.do")
	public String deleteStore(HttpServletRequest request) {
		System.out.println("删除店铺信息！！！！！！！！！！————>deleteStore");
		int store_id = Integer.valueOf(request.getParameter("store_id"));
		if (storeService.deleteStore(store_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getStoreList.do?sname=base"; 
	}
}
